import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper';

let root = null;

function render(props = {}) {
  const { container } = props;
  const domElement = container ? container.querySelector('#root') : document.getElementById('root');
  root = createRoot(domElement);
  root.render(<App />);
}

renderWithQiankun({
  mount(props) {
    console.log('[react] props from main framework', props);
    render(props);
  },
  bootstrap() {
    console.log('[react] react app bootstraped');
  },
  unmount(props) {
    console.log('[react] react app unmount');
    root?.unmount();
    root = null;
  }
});

// 独立运行时
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({});
}